package com.qxbb.report.imgUtils;
import com.qxbb.report.utils.StringUtils;
import com.qxbb.report.utils.http.HttpResult;
import lombok.extern.java.Log;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/image")
@Log
public class ImageController {

    /**
     * 单图片文件压缩，指定压缩大小
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/file/img")
    public String imgUploads(@RequestParam("file") MultipartFile[] file) throws IOException {
        for (MultipartFile multipartFile : file) {
            //压缩图片到指定120K以内,不管你的图片有多少兆,都不会超过120kb,精度还算可以,不会模糊
            log.info(multipartFile.getBytes().toString());
            byte[] bytes = PicUtils.compressPicForScale(multipartFile.getBytes(), 200);
            //生成保存在服务器的图片名称，统一修改原后缀名为:jpg
            String newFileName = UUID.randomUUID() + ".jpg";
            File fOut = new File("C:\\Users\\Administrator\\Desktop\\ImgFiles\\" + newFileName);
            FileOutputStream fileOutputStream = new FileOutputStream(fOut);
            fileOutputStream.write(bytes);
            fileOutputStream.close();

        }
       return null;
    }

    /**
     * 单图片文件压缩，不能指定压缩大小
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/file/img2")
    public String imgUploads2(@RequestParam("file") MultipartFile file) throws IOException {
        byte[] bs = ImgCompression.getImageCom(file);
        String newFileName = UUID.randomUUID() + ".jpg";
        File fOut = new File("C:\\image\\copy\\" + newFileName);
        FileOutputStream fileOutputStream = new FileOutputStream(fOut);
        fileOutputStream.write(bs);
        fileOutputStream.close();
        return newFileName;
    }

    /**
     * 向指定文件夹压缩图片
     * @return
     */
    @PostMapping("/zipImgFile")
    public HttpResult  zipImgFile(){
        try {
            String imgFileName = "C:\\Users\\Administrator\\Desktop\\图片";   //原图片文件夹
            String imgZipFileName = "C:\\Users\\Administrator\\Desktop\\ImgFiles";  // 解压图片文件夹
            // 图片文件夹
            File imgFile = new File(imgFileName);
            File zipFile = new File(imgZipFileName);
            String fileList[] = imgFile.list(); // 原图片文件夹所有文件
            String zipFileList[] = zipFile.list(); // 解压文件夹所有文件
            List<String> zipList = Arrays.asList(zipFileList);
            // 拿到所有文件
            if (StringUtils.isNotEmpty(fileList)){
                for (String fileName : fileList) {
                    if (!zipList.contains(fileName)) {  // 不存在就压缩
                        File file = new File(imgFileName + "\\" + fileName);
                        FileInputStream inputStream = new FileInputStream(file);

                        byte[] bytes = new byte[10500240];
                        while (true) {
                            int len = inputStream.read(bytes);
                            if (len == -1) {
                                break;
                            }
                        }
                        inputStream.close();
                        byte[] imgBytes = PicUtils.compressPicForScale(bytes, 200);
                        File fOut = new File("C:\\Users\\Administrator\\Desktop\\ImgFiles\\" + fileName);
                        FileOutputStream fileOutputStream = new FileOutputStream(fOut);
                        fileOutputStream.write(imgBytes);
                        fileOutputStream.close();
                    }
                }
            }
            return HttpResult.ok("压缩成功");
        } catch (Exception e) {
            throw new RuntimeException("图片压缩失败:{}",e);
        }
    }


    public static void main(String[] args) throws IOException {

            String imgFileName = "C:\\Users\\Administrator\\Desktop\\图片";   //原图片文件夹
            String imgZipFileName = "C:\\Users\\Administrator\\Desktop\\ImgFiles";  // 解压图片文件夹
            // 图片文件夹
            File imgFile = new File(imgFileName);
            File zipFile = new File(imgZipFileName);
            String fileList[] = imgFile.list(); // 原图片文件夹所有文件
            String zipFileList[] = zipFile.list(); // 解压文件夹所有文件
            List<String> zipList = Arrays.asList(zipFileList);
            // 拿到所有文件
            if (StringUtils.isNotEmpty(fileList)){
                for (String fileName : fileList) {
                    if (!zipList.contains(fileName)) {  // 不存在就压缩
                        File file = new File(imgFileName + "\\" + fileName);
                        FileInputStream inputStream = new FileInputStream(file);

                        byte[] bytes = new byte[10500240];
                        while (true) {
                            int len = inputStream.read(bytes);
                            if (len == -1) {
                                break;
                            }
                        }
                        inputStream.close();
                        byte[] imgBytes = PicUtils.compressPicForScale(bytes, 200);
                        File fOut = new File("C:\\Users\\Administrator\\Desktop\\ImgFiles\\" + fileName);
                        FileOutputStream fileOutputStream = new FileOutputStream(fOut);
                        fileOutputStream.write(imgBytes);
                        fileOutputStream.close();
                    }
                }
            }


    }

}
